function loadCase2Page() {
    var apiURL = '/api/Case/GetCase';
    var _caseID = $(document).getUrlParam("caseID");    
    console.log(_caseID);
    
    $.ajax({
        url: '/api/Case/GetImagePath',
        type: 'GET',
        dataType: 'json',
        data: { caseID: _caseID },
    })
    .done(function (data) {
        var imgPath = '/api/Case/GetImage?path=' + data;
        $('img#uploadedImg').attr('src', imgPath);
        console.log("image loaded successfully");
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });    

    $.ajax({
        url: apiURL,
        type: 'GET',
        dataType: 'json',
        data: { caseID: _caseID },
    })
    .done(function (data) {
        var idTrunk = data.caseID.substring(0, 12);
        $('#caseID').append(idTrunk);
        $('#caseStatus').append(data.caseStatus);
        if(data.caseStatus==="Closed")
        {
            $('#closureDescDiv').show();
            $("#closureDesc").append(data.closureDesc);
        }
        $('#fbDate').append(data.feedbackDate);
        $('#recDate').append(data.receivedDate);
        $('#dueDate').append(data.dueDate);
        if(data.closedDate===""){
            $('#closedDate').append('NA')
        }else{
            $('#closedDate').append(data.closedDate)            
        }
        $('#sbjTitle').append(data.subject);
        $('#fbType').append(data.feedbackType);
        $('#channel').append(data.channel);
        $('#caseDesc').append(data.description);
        $('#agency').append(data.caseOwner);
        $('#category').append(data.category);
        $('#subCate').append(data.subCategory1);
        $('#address').append(data.customerAddress);
        $('#postal').append(data.postalCode);
        $('#lastUpdateDate').append(data.lastUpdateDateTime);
        $('#lastUpdateAgency').append(data.lastUpdateAgency);            
        if(data.lastUpdateAgency==='NA')
        {
            $('#lastAction').append('Case is created by Mobile App user');
        }else{
            $('#lastAction').append('Case modified by' + data.lastUpdateAgency);
        }
        $('#custName').append(data.custName);
        $('#phone').append(data.phone);
        $('#email').append(data.email);
        var mCustID = data.mCustID;
        console.log(mCustID);
        $("#mCustID").append(mCustID);
        

        console.log("case info loaded successfully");
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });    
}

function loadImage() {
    var _caseID = $(document).getUrlParam("caseID");    
    console.log(_caseID);

    $.ajax({
        url: '/api/Case/GetImagePath',
        type: 'GET',
        dataType: 'json',
        data: { caseID: _caseID },
    })
    .done(function (data) {
        var imgPath = '/api/Case/GetImage?path=' + data;
        console.log(imgPath);
        console.log("success");
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });    
}

function submitReply() {
    var reply = $('#panelReply-caseReply').val();
    var _caseID = $(document).getUrlParam("caseID");
    var replyType = $("input[name=replyType]:checked").val();
    var _caseStatus;
    var closedDate;
    if (replyType==='1')
    {
        _caseStatus = "Closed";
        closedDate = $.datepicker.formatDate("d-M-yy",new Date());
    }
    else
    {
        _caseStatus = "Open"
    }
    console.log("Variables in submitReply(): "
        +"\nreply: "+reply
        +"\n_caseID: "+_caseID
        +"\nreplyType: "+replyType
        +"\n_caseStatus: "+_caseStatus
        +"\nclosedDate: "+closedDate);

    var updateObj = {
        caseID:_caseID,
        closureDesc:reply,
        caseStatus:_caseStatus,
        closedDate:closedDate
    }

    $.ajax({
        url: '/api/Case/PostCaseDesc',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(updateObj),
    })
    .done(function (data) {
        alert(data.IsSuccess);
        console.log("success");
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });
}

function acquireNewCaseID(){
    $.ajax({
        url: '/api/Case/GetNewCaseID',
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json'

    })
    .done(function(data) {
        $('#caseID').append(data);
        var idTrunk = data.substring(0, 12);
        console.log(idTrunk);
        $('#caseIDTrunk').append(idTrunk);
        console.log("acquired case id successfully");
    })
    .fail(function() {
        console.log("acquired case id error");
    })
    .always(function() {
        console.log("acquired case id complete");
    });
    
}

function preFillForm() {
    var _caseID = $(document).getUrlParam("caseID");    
    console.log(_caseID);
    
    $.ajax({
        url: '/api/Case/GetImagePath',
        type: 'GET',
        dataType: 'json',
        data: { caseID: _caseID },
    })
    .done(function (data) {
        var imgPath = '/api/Case/GetImage?path=' + data;
        $('img#uploadedImg').attr('src', imgPath);
        console.log("image loaded successfully");
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });    

    $.ajax({
        url: '/api/Case/GetCase',
        type: 'GET',
        dataType: 'json',
        data: { caseID: _caseID },
    })
    .done(function (data) {
        var idTrunk = data.caseID.substring(0, 12);
        $('#caseID').val(data.caseID);
        $('#caseStatus').val(data.caseStatus);
        if (data.caseStatus==="Closed") {
            $("#closedDateDiv").show();
            $("#closedDate").val(data.closedDate);
            $("#closureDescDiv").show();
            $("#closureDesc").val(data.closureDesc);
        }else{
            $("#closedDateDiv").hide();
            $("#closedDate").val("");
            $("#closureDescDiv").hide();
            $("#closureDesc").val("");
        }

        $('#fbDate').val(data.feedbackDate);
        $('#recDate').val(data.receivedDate);
        $('#dueDate').val(data.dueDate);
        if(data.closedDate===""){
            $('#closedDate').val("");
        }else{
            $('#closedDate').val(data.closedDate);         
        }
        $('#sbjTitle').val(data.subject);
        $('#fbType').val(data.feedbackType);
        $('#channel').val(data.channel);
        $('#caseDesc').val(data.description);
        $('#agency').val(data.caseOwner);
        $('#autoComp-category').val(data.category);
        $('#autoComp-subCate').val(data.subCategory1);
        $('#address').val(data.customerAddress);
        $('#postal').val(data.postalCode);
        $('#lastUpdateDate').val(data.lastUpdateDateTime);
        $('#lastUpdateAgency').val(data.lastUpdateAgency);            
        
        $('#custName').val(data.custName);
        $('#phone').val(data.phone);
        $('#email').val(data.email);
        $("#mCustID").val(data.mCustID);
        $("#NRIC").val(data.NRIC);

        console.log("case info loaded successfully");
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });    
}

function updateCaseInfo() {
    var _caseID = $(document).getUrlParam("caseID");
    var today = $.datepicker.formatDate('d-M-yy', new Date());
    var caseID = $("#caseID").val(); 
    var agencyCaseID = "";
    var feedbackDate = $("#fbDate").val();
    var receivedDate = $("#recDate").val();
    var dueDate = $("#dueDate").val();
    var closedDate = $("#closedDate").val()== 'NA'? "" : $("#closedDate").val();
    var closureDesc = $("#closureDesc").val();
    var mCustID = $("#mCustID").val();
    var custName = $("#custName").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var NRIC = $("#NRIC").val();
    var caseStatus = $("#caseStatus").val();
    var category = $("#autoComp-category").val();
    var subCategory1 = $("#autoComp-subCate").val();
    var subCategory2 = "";
    var feedbackType = $("#feedbackType").val();
    var channel = $("#channel").val();
    var caseAddress = $("#address").val();
    var customerAddress = $("#address").val();
    var postalCode = $("#postalCode").val();
    var longitude = "";
    var latitude = "";
    var subject = $("#sbjTitle").val();
    var description = $("#caseDesc").val();
    var source = "";
    var timesRouted = "";
    var isViewed = 1;
    var caseOwner = $("#autoComp-agency").val();
    var jointID = "";
    var jointDescription = "";
    var lastUpdateDateTime = today;
    var lastUpdateAgency = "current agency";
    var duplicateMasterCaseID = "";
    var duplicateID = "";
    var imagePath ="" ;
    $.ajax({
        url: '/api/Case/GetImagePath',
        type: 'GET',
        dataType: 'json',
        data: { caseID: _caseID },
    })
    .done(function (data) {
        imagePath = '/api/Case/GetImage?path=' + data;
        //$('img#uploadedImg').attr('src', imgPath);
        console.log("image path is loaded again successfully");
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });

    var updateObj = {
        caseID:_caseID,
        closureDesc:closureDesc,
        agencyCaseID:agencyCaseID,
        feedbackDate:feedbackDate,
        receivedDate:receivedDate,
        dueDate:dueDate,
        closedDate:closedDate,
        mCustID:mCustID,
        custName:custName,
        email:email,
        phone:phone,
        NRIC:NRIC,
        caseStatus:caseStatus,
        category:category,
        subCategory1:subCategory1,
        subCategory2:subCategory2,
        feedbackType:feedbackType,
        channel:channel,
        caseAddress:caseAddress,
        customerAddress:customerAddress,
        postalCode:postalCode,
        longitude:longitude,
        latitude:latitude,
        subject:subject,
        description:description,
        source:source,
        isViewed:isViewed,
        timesRouted:timesRouted,
        caseOwner:caseOwner,
        jointID:jointID,
        jointDescription:jointDescription,
        lastUpdateDateTime:lastUpdateDateTime,
        lastUpdateAgency:lastUpdateAgency,
        duplicateMasterCaseID:duplicateMasterCaseID,
        duplicateID:duplicateID,
        imagePath:imagePath
    }

    $.ajax({
        url: '/api/Case/UpdateCase',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(updateObj)
    })
    .done(function (data) {
        console.log("success");
        alert(data.IsSuccess);
        
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });
}

function createNewCase() {
    var _caseID = $(document).getUrlParam("caseID");
    var today = $.datepicker.formatDate('d-M-yy', new Date());
    var caseID = $("#caseID").val(); 
    var agencyCaseID = "";
    var feedbackDate = $("#fbDate").val();
    var receivedDate = $("#recDate").val();
    var dueDate = $("#dueDate").val();
    var closedDate = $("#closedDate").val()== 'NA'? "" : $("#closedDate").val();
    var closureDesc = $("#closureDesc").val();
    var mCustID = $("#mCustID").val();
    var custName = $("#custName").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var NRIC = $("#NRIC").val();
    var caseStatus = $("#caseStatus").val();
    var category = $("#autoComp-category").val();
    var subCategory1 = $("#autoComp-subCate").val();
    var subCategory2 = "";
    var feedbackType = $("#feedbackType").val();
    var channel = $("#channel").val();
    var caseAddress = $("#address").val();
    var customerAddress = $("#address").val();
    var postalCode = $("#postalCode").val();
    var longitude = "";
    var latitude = "";
    var subject = $("#sbjTitle").val();
    var description = $("#caseDesc").val();
    var source = "";
    var timesRouted = "";
    var isViewed = 1;
    var caseOwner = $("#autoComp-agency").val();
    var jointID = "";
    var jointDescription = "";
    var lastUpdateDateTime = today;
    var lastUpdateAgency = "current agency";
    var duplicateMasterCaseID = "";
    var duplicateID = "";
    var imagePath ="" ;
    $.ajax({
        url: '/api/Case/GetImagePath',
        type: 'GET',
        dataType: 'json',
        data: { caseID: _caseID },
    })
    .done(function (data) {
        imagePath = '/api/Case/GetImage?path=' + data;
        //$('img#uploadedImg').attr('src', imgPath);
        console.log("image path is loaded again successfully");
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });

    var updateObj = {
        caseID:_caseID,
        closureDesc:closureDesc,
        agencyCaseID:agencyCaseID,
        feedbackDate:feedbackDate,
        receivedDate:receivedDate,
        dueDate:dueDate,
        closedDate:closedDate,
        mCustID:mCustID,
        custName:custName,
        email:email,
        phone:phone,
        NRIC:NRIC,
        caseStatus:caseStatus,
        category:category,
        subCategory1:subCategory1,
        subCategory2:subCategory2,
        feedbackType:feedbackType,
        channel:channel,
        caseAddress:caseAddress,
        customerAddress:customerAddress,
        postalCode:postalCode,
        longitude:longitude,
        latitude:latitude,
        subject:subject,
        description:description,
        source:source,
        isViewed:isViewed,
        timesRouted:timesRouted,
        caseOwner:caseOwner,
        jointID:jointID,
        jointDescription:jointDescription,
        lastUpdateDateTime:lastUpdateDateTime,
        lastUpdateAgency:lastUpdateAgency,
        duplicateMasterCaseID:duplicateMasterCaseID,
        duplicateID:duplicateID,
        imagePath:imagePath
    }

    $.ajax({
        url: '/api/Case/UpdateCase',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(updateObj)
    })
    .done(function (data) {
        console.log("success");
        alert(data.IsSuccess);
        
    })
    .fail(function () {
        console.log("error");
    })
    .always(function () {
        console.log("complete");
    });
}